import csv
import requests
from lxml import etree
heapq={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36'}
def get_html(url):
    try:
        r=requests.get(url=url,headers=heapq)
        r.encoding=r.apparent_encoding
        r.raise_for_status()
        return r.text
    except Exception as error:
        print(error)
def parser(html):
    doc=etree.HTML(html)
    out_list=[]
    for row in doc.xpath('//*[@id="tab-book"]/div[2]/div[3]/div/ul/li'):
        row_data=[
            row.xpath("div[2]/h4/a/text()")[0],#书名
            row.xpath("div[2]/div/span/text()")[0],#作者
            row.xpath("div[2]/span/span/text()")[0],#价格
            row.xpath("div[2]/h4/a/@href")[0]#详情链接
            ]
        out_list.append(row_data)
    print(out_list)
    return out_list
def save_csv(itme,path):
    with open(path,"a+",newline='',encoding="utf-8")as f:
        csv_writ = csv.writer(f)
        csv_writ.writerows(itme)
if __name__ == '__main__':
    url='https://www.ryjiaoyu.com/book'
    html=get_html(url)
    out_list= parser(html)
    save_csv(out_list,"qidian.csv")


